﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using System.IO;
using Microsoft.DataWarehouse.VsIntegration.Shell.Project.Extensibility;

namespace SSISPackageExplorer
{
    public class Session
    {
        public static List<Project> GetProjectList()
        {
            List<Project> result = new List<Project>();

            if (Connect.VisualStudioApplication.Solution == null)
                return result;

            if (Connect.VisualStudioApplication.Solution.Projects.Count == 0)
                return result;
                        
            foreach (object projObject in Connect.VisualStudioApplication.Solution.Projects)
            { 
                if (!(projObject is ProjectExt))
                    continue;

                result.Add((Project)projObject);
            }

            return result;
        }

        public static bool HasProject() 
        {
            return Session.GetProjectList().Count > 0;       
        }

        public static bool HasReadonlyConfigFile() 
        {
            foreach (Project proj in GetProjectList())
            {
                string file = GetProjectConfigFile(proj);
                if (File.Exists(file) && new FileInfo(file).IsReadOnly)
                {
                    return true;                
                }
            }

            return false;
        }

        public static string GetProjectConfigFile(Project project)
        {
            return Path.GetDirectoryName(project.FileName) + "\\SSISPackageExplorerConfig.xml";        
        }
    }
}
